package com.example.lcode.backtrack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Question47 {

    public List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        boolean []vis = new boolean[nums.length];
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> re = new ArrayList<>();
        permute(nums, res, re, vis, 0);
        return res;
    }

    private void permute(int[] nums, List<List<Integer>> res, List<Integer> re ,boolean[] vis, int startIndex) {
        if (re.size() == nums.length) {
            res.add(new ArrayList<>(re));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if (vis[i] == true) {
                continue;
            }
            if (i > 0 && nums[i - 1] == nums[i] && vis[i - 1]) {
                continue;
            }
            vis[i] = true;
            re.add(nums[i]);
            permute(nums, res, re, vis, i);
            vis[i] = false;
            re.remove(re.size() - 1);
        }
    }

    public static void main(String[] args) {
        int []arr = {1,1,2};
        Question47 question47 = new Question47();
        List<List<Integer>> permute = question47.permuteUnique(arr);
        System.out.println(permute);
    }

}
